//
//  SaleOrderModel.m
//  quickBatchManager
//
//  Created by jack on 15/10/8.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "SaleOrderModel.h"
#import "UploadData.h"

@implementation SaleOrderModel
+(NSString *)getTableName
{
    return @"qbm_sale_order";
}

+(NSString *)getPrimaryKey
{
    return @"saleOrderId";
}

-(void)setClientId:(NSInteger)clientId
{
    _clientId=clientId;
    _client=[ClientModel searchSingleWithWhere:@{@"clientId":[ToolUtils numberToStr:clientId]} orderBy:nil];

}


+(void)columnAttributeWithProperty:(LKDBProperty *)property
{
    if([property.sqlColumnName isEqualToString:@"saleOrderNo"])
    {
        property.isUnique = YES;
    }
    
    else if([property.sqlColumnName isEqualToString:@"discount"])
    {
        property.defaultValue = @"1";
    }
    
    
}



+(void)dbDidInserted:(NSObject*)entity result:(BOOL)result
{
    if([entity isKindOfClass:[SaleOrderModel class]])
    {
        SaleOrderModel *model = (SaleOrderModel *)entity;
        UploadData *data = [[UploadData alloc] init];
        data.t_table = [SaleOrderModel getTableName];
        data.t_opType = DB_INSERT;
        data.t_pkName = [self getPrimaryKey];
        data.t_pk = model.rowid;
        data.t_time = [[NSDate date] timeIntervalSince1970InMilliSecond];
        model.saleOrderId = data.t_pk;
         NSMutableDictionary *params = model.keyValues;
        [params removeObjectForKey:@"client"];
        NSString *value = [self DataTOjsonString:params];
        data.content = value;
        [UploadData insertToDB:data];
    }
 //   NSLog(@"DB 插入数据成功");
    
    
}

+(void)dbDidUpdated:(NSObject*)entity result:(BOOL)result
{
    if([entity isKindOfClass:[SaleOrderModel class]])
    {
        SaleOrderModel *model = (SaleOrderModel *)entity;
        UploadData *data = [[UploadData alloc] init];
        data.t_table = [SaleOrderModel getTableName];
        data.t_opType = DB_UPDATE;
        data.t_pkName = [self getPrimaryKey];
        data.t_pk = model.rowid;
        data.t_time = [[NSDate date] timeIntervalSince1970InMilliSecond];
        model.saleOrderId = data.t_pk;
        NSMutableDictionary *params = model.keyValues;
        [params removeObjectForKey:@"client"];
        NSString *value = [self DataTOjsonString:params];
        data.content = value;
        [UploadData insertToDB:data];
    }
  //  NSLog(@"DB 更新数据");
}


+(void)dbDidDeleted:(NSObject*)entity result:(BOOL)result
{
    if([entity isKindOfClass:[SaleOrderModel class]])
    {
        SaleOrderModel *model = (SaleOrderModel *)entity;
        UploadData *data = [[UploadData alloc] init];
        data.t_table = [SaleOrderModel getTableName];
        data.t_opType = DB_DELETE;
        data.t_pkName = [self getPrimaryKey];
        data.t_pk = model.rowid;
        data.t_time = [[NSDate date] timeIntervalSince1970InMilliSecond];
        model.saleOrderId = data.t_pk;
        NSMutableDictionary *params = model.keyValues;
        [params removeObjectForKey:@"client"];
        NSString *value = [self DataTOjsonString:params];
        data.content = value;
        [UploadData insertToDB:data];
    }
 //   NSLog(@"DB 删除数据");
}

@end
